{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt_file = \"step2_result.txt\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 清空文本\n",
    "with open(txt_file, \"w\", encoding=\"utf-8\") as file: \n",
    "    file.write(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_result_text(text, number, unit=\"\", decimal=4): \n",
    "    result = f\"{text} {round(number, decimal)} {unit}\" \n",
    "    return result\n",
    "\n",
    "def save_result_to_txt(result): \n",
    "    with open(txt_file, \"a\", encoding=\"utf-8\") as file: \n",
    "        file.write(result + \"\\n\") \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 算一对Block的PE时间\n",
    "\n",
    "可从step0-1.py得出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "pair_block_time = 1.8006\n",
    "save_result_to_txt(\"1 一对Block的时间： \" + str(pair_block_time) + \"s\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 算PE的总时间（without delay）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# def calculate_time(tlc_gears, num_gears, pair_block_time):\n",
    "#     total_time = sum(tlc_gears) * num_gears * pair_block_time\n",
    "#     return total_time\n",
    "\n",
    "tlc_gears = [3, 300, 1000, 3000, 4500]\n",
    "num_gears = 6\n",
    "\n",
    "pe_time = sum(tlc_gears) * num_gears * pair_block_time\n",
    "pe_time_in_hours = pe_time / 3600\n",
    "\n",
    "save_result_to_txt(f\"\\n2 算PE的总时间\")\n",
    "save_result_to_txt(format_result_text(\"Total time: \", pe_time, \"s\"))\n",
    "save_result_to_txt(format_result_text(\"Total time in hours: \", pe_time_in_hours, \"h\", 4))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 算Delay的总时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入\n",
    "# pair_block_time = pair_block_time\n",
    "delay_time = 100  # delay的时间\n",
    "num_pairs = 24  # 需要delay的Block对的数量\n",
    "num_delays = 297  # 总共delay的次数\n",
    "\n",
    "# 计算\n",
    "one_delay_time = delay_time - (pair_block_time * (num_pairs - 1))\n",
    "total_delay_time = num_delays * one_delay_time\n",
    "total_delay_time_in_hour = total_delay_time / 3600\n",
    "\n",
    "# 输出\n",
    "save_result_to_txt(f\"\\n3 算Delay的总时间\")\n",
    "save_result_to_txt(format_result_text(\"一次delay的时间 =\", one_delay_time, \"s\"))\n",
    "save_result_to_txt(format_result_text(\"总共delay的时间 =\", total_delay_time, \"s\"))\n",
    "save_result_to_txt(format_result_text(\"换算成小时的结果是\", total_delay_time_in_hour, \"h\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 总时间\n",
    "\n",
    "总时间 = PE时间 + delay时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_time = pe_time + total_delay_time\n",
    "total_time_in_hour = total_time / 3600\n",
    "\n",
    "save_result_to_txt(f\"\\n4 算总时间\")\n",
    "save_result_to_txt(format_result_text(\"总时间 = \", total_time, \"s\"))\n",
    "save_result_to_txt(format_result_text(\"总时间 = \", total_time_in_hour, \"h\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
